package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : BrandController  //类名
 * @Description : 品牌的controller  //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-16 19:47  //时间
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;
    private final CategoryBrandService categoryBrandService;

    public BrandController(BrandService brandService, CategoryBrandService categoryBrandService) {
        this.brandService = brandService;
        this.categoryBrandService = categoryBrandService;
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        /**
         * @Description //TODO 根据id查询品牌
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<com.leyou.item.dto.BrandDTO>
         */
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }
    
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandIds(@RequestParam("ids") List<Long> idList) {
        /**
         * @Description //TODO 根据品牌id集合查询品牌集合
         * @Param [idList]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.BrandDTO>>
         */
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(idList)));
    }
    
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                              @RequestParam(value = "key" ,required = false)String key){
        /**
         * @Description //TODO 分页查询品牌
         * @Param [page, rows, key]
         * @return org.springframework.http.ResponseEntity<com.leyou.common.dto.PageDTO < com.leyou.item.dto.BrandDTO>>
         */
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,key));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandEntity(@RequestParam("id")Long id){
        /**
         * @Description //TODO 根据分类id查询品牌
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.BrandDTO>>
         */
        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        /**
         * @Description //TODO 新增品牌
         * @Param [brandDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        /**
         * @Description //TODO 更新品牌
         * @Param [brandDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        /**
         * @Description //TODO 删除品牌
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        //删除品牌表
        brandService.removeById(id);
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
